#include "Network/PackageWriter.h"

namespace PocketSurvival
{
    void PackageWriter::writePoint2(const Point2 &point)
    {
        writeInt32(point.posX);
        writeInt32(point.posY);
    }
    void PackageWriter::writePoint3(const Point3 &point)
    {
        writeInt32(point.posX);
        writeInt32(point.posY);
        writeInt32(point.posZ);
    }
    void PackageWriter::writeBlockPoint3(const Point3 &point)
    {
        writeInt32(point.posX);
        writeUInt8((uint8_t)(point.posY & 0xFF));
        writeInt32(point.posZ);
    }
    void PackageWriter::writeVector2(const Vector2 &vector)
    {
        writeFloat(vector.posX);
        writeFloat(vector.posY);
    }
    void PackageWriter::writeVector3(const Vector3 &vector)
    {
        writeFloat(vector.posX);
        writeFloat(vector.posY);
        writeFloat(vector.posZ);
    }
    void PackageWriter::writeQuaternion(const Quaternion &quaternion)
    {
        writeFloat(quaternion.X);
        writeFloat(quaternion.Y);
        writeFloat(quaternion.Z);
        writeFloat(quaternion.W);
    }
    void PackageWriter::writeRay3(const Ray3 &ray3)
    {
        writeVector3(ray3.Position);
        writeVector3(ray3.Direction);
    }
    void PackageWriter::writeCellFace(const CellFace &cellFace)
    {
        writeBlockPoint3(cellFace.point);
        writeUInt8((uint8_t)(cellFace.face & 0xFF));
    }
    void PackageWriter::writeColor4(const Color4 &color)
    {
        writeUInt8(color.R);
        writeUInt8(color.G);
        writeUInt8(color.B);
        writeUInt8(color.A);
    }
    void PackageWriter::writeTerrainRaycastResult(const TerrainRaycastResult &terrainRaycastResult)
    {
        writeRay3(terrainRaycastResult.ray);
        writeInt32(terrainRaycastResult.value);
        writeInt32(terrainRaycastResult.collisionBoxIndex);
        writeFloat(terrainRaycastResult.distance);
        writeCellFace(terrainRaycastResult.cellFace);
    }
    void PackageWriter::writeMatrix(const Matrix &matrix)
    {
        writeFloat(matrix.M11);
        writeFloat(matrix.M21);
        writeFloat(matrix.M31);
        writeFloat(matrix.M41);

        writeFloat(matrix.M12);
        writeFloat(matrix.M22);
        writeFloat(matrix.M32);
        writeFloat(matrix.M42);

        writeFloat(matrix.M13);
        writeFloat(matrix.M23);
        writeFloat(matrix.M33);
        writeFloat(matrix.M43);

        writeFloat(matrix.M14);
        writeFloat(matrix.M24);
        writeFloat(matrix.M34);
        writeFloat(matrix.M44);
    }

    void PackageWriter::writeGuid(const GameGuid &gameGuid)
    {
        writeUInt8(sizeof(GameGuid));
        write(gameGuid.constData(), sizeof(GameGuid));
    }
} // namespace PocketSurvival
